package com.zyh.study.ForestPlus;

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * security-trade-platform-
 *
 * @author yuanHua.zhang
 * @since 2025-09-08 16:39
 */
/**
 * 基于 Redisson 的缓存服务实现
 */
@Component
public class RedissonCacheService implements CacheService {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void set(String key, Object value) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        bucket.set(value);
    }

    @Override
    public void set(String key, Object value, long ttl, TimeUnit timeUnit) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        bucket.set(value, ttl, timeUnit);
    }

    @Override
    public boolean trySet(String key, Object value, long ttl, TimeUnit timeUnit) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        return bucket.trySet(value, ttl, timeUnit);
    }

    @Override
    public <T> T get(String key, Class<T> clazz) {
        RBucket<T> bucket = redissonClient.getBucket(key);
        return bucket.get();
    }

    @Override
    public boolean exists(String key) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        return bucket.isExists();
    }

    @Override
    public void delete(String key) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        bucket.delete();
    }

    @Override
    public Iterable<String> getKeysByPattern(String pattern) {
        return redissonClient.getKeys().getKeysByPattern(pattern);
    }
}
